﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASoft.Base;
using ASoft.Web.Base;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Asoft.Web.Controllers
{
    public class RoomDataController : EntityController<WebRoomDataRepository, WebRoomData>
    {
        protected override PageResult<WebRoomData> GetIndex1PageResult()
        {
            var condition = new Dictionary<string, string>();
            condition["customcode"] = CurrentContext.CustomCode;
            var result = Repository.GetPageResult(condition, PageIndex, PageSize, new string[] { "id" }, new string[] { "asc" });
            return result;
        }
        protected override void BeforeIndex1ReturnView()
        {
            ViewData["wsd"] = this.Repository.GetWSData(CurrentContext.CustomCode);
            ViewData["reloadsecond"] = ConfigInfo.GetValue<int>("reloadSecond", 1);
        }

        public override ActionResult Index()
        {
            this.ViewData["reloadSecond"] = ConfigInfo.GetValue<int>("reloadSecond", 1);
            return base.Index();
        }


        protected override void ProcessJQGridEdit(WebRoomData entity)
        {

        }

        [HttpPost]
        public ActionResult Upload(WebRoomData entity)
        {
            if (entity != null && entity.Id > 0)
                this.Repository.Insert(entity);
            return Content("OK");
        }

        [HttpPost]
        public ActionResult UploadWS(WeatherStationData entity)
        {
            if (entity != null)
                this.Repository.Insert(entity);
            return Content("OK");
        }


        public ActionResult R(string customCode)
        {
            string result = "R/01";
            if (string.IsNullOrWhiteSpace(customCode))
            {
                customCode = "01";
            }
            for (int i = 0; i < 100; i++)
            {
                var entity = new WebRoomData();
                entity.Id = this.Repository.GetEntities(null).Count + 1;
                entity.CustomCode = customCode;
                entity.时间 = DateTime.Now.AddDays(i);
                entity.RoomName = entity.Id.ToString();
                this.Repository.Insert(entity);
            }
            return Content("R");
        }

        public ActionResult W(string customCode)
        {
            string result = "W/01";
            if (string.IsNullOrWhiteSpace(customCode))
            {
                customCode = "01";
            }
            for (int i = 0; i < 100; i++)
            {
                WeatherStationData w = new WeatherStationData();
                w.Id = this.Repository.GetEntities(null).Count + 1;
                w.CustomCode = customCode;
                w.时间 = DateTime.Now;
                w.风向 = 4;
                w.风速 = 10;
                w.光照 = 30;
                w.雨雪 = 1;
                this.Repository.Insert(w);
            }
            return Content("W");
        }



        public ActionResult GetWS()
        {
            var wsData = this.Repository.GetWSData(CurrentContext.CustomCode);
            var result = JsonConvert.SerializeObject(wsData);
            return Content(result);
        }

        protected override void OnBeforeJQGridDatasGetCondition(Dictionary<string, string> condition)
        {
            base.OnBeforeJQGridDatasGetCondition(condition);
            condition["CustomCode"] = CurrentContext.CustomCode.ToLower();
        }
    }
}
